﻿Imports System.Runtime.InteropServices

Public Class RHibernate

#Region "Var"
    Dim force2close As Boolean
    Dim isLoaded As Boolean = True
    Dim hibernateTimeSetting() As String = My.Settings.HiberniteTime.Split(":")
    Dim now As DateTime = DateTime.Now
    Dim hibernateTime As New DateTime(now.Year, now.Month, now.Day, hibernateTimeSetting(0), hibernateTimeSetting(1), 0)
    Enum State
        LogOff
        Lock
        StandBy
        ShutDown
        Restart
        Hibernate
    End Enum
#End Region

#Region "Func"
    <DllImport("user32.dll")>
    Public Shared Sub LockWorkStation()
    End Sub

    <DllImport("user32.dll")>
    Public Shared Function ExitWindowsEx(uFlags As Integer, dwReason As Integer) As Integer
    End Function

    Sub DoTask(status As State)
        Select Case status
            Case State.LogOff
                ExitWindowsEx(0, 0)
            Case State.Lock
                LockWorkStation()
            Case State.StandBy
                Application.SetSuspendState(PowerState.Suspend, True, True)
            Case State.ShutDown
                ExitWindowsEx(1, 0)
            Case State.Restart
                ExitWindowsEx(2, 0)
            Case State.Hibernate
                Application.SetSuspendState(PowerState.Hibernate, True, True)
        End Select
    End Sub
#End Region

#Region "EventHandler MenuStrip"
    Private Sub ni_DoubleClick(sender As System.Object, e As System.EventArgs) Handles ni.DoubleClick
        If Me.Visible Then
            Me.Hide()
        Else
            Me.Show()
            Me.WindowState = FormWindowState.Normal
        End If
    End Sub

    Private Sub tsShowForm_Click(sender As System.Object, e As System.EventArgs) Handles tsShowForm.Click
        ni_DoubleClick(sender, e)
    End Sub

    Private Sub tsExit_Click(sender As System.Object, e As System.EventArgs) Handles tsExit.Click
        force2close = True
        Application.Exit()
    End Sub

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        If DateTime.Now > hibernateTime Then
            Timer1.Stop()
            Dim f As New Confirm()
            Dim result As DialogResult = f.ShowDialog()
            If result = DialogResult.OK Then
                Me.Hide()
                hibernateTime = hibernateTime.AddDays(1)
                Timer1.Interval = 60000
                DoTask(State.Hibernate)
            ElseIf result = DialogResult.Retry Then
                Timer1.Interval = My.Settings.PendingInterval
            End If
            Timer1.Start()
        End If
    End Sub

    Private Sub Timer2_Tick(sender As System.Object, e As System.EventArgs) Handles Timer2.Tick
        If isLoaded Then
            Me.Hide()
            Me.Opacity = 1
            Me.ShowInTaskbar = True
            Timer2.Stop()
        End If
    End Sub

    'Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    '    If DateTime.Now > hibernateTime Then
    '        DoTask(State.StandBy)
    '    End If
    'End Sub
#End Region

#Region "EventHandler Form"
    Private Sub RHibernate_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If force2close = False Then
            Me.WindowState = FormWindowState.Minimized
            Me.Hide()
            e.Cancel = True
        End If
    End Sub

    Private Sub RHibernate_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Timer2.Start()
        txtHibernateTime.Text = My.Settings.HiberniteTime
        If DateTime.Now > hibernateTime Then
            hibernateTime = hibernateTime.AddDays(1)
        End If
        isLoaded = True
    End Sub

    Private Sub RHibernate_Resize(sender As System.Object, e As System.EventArgs) Handles MyBase.Resize
        If Me.WindowState = FormWindowState.Minimized Then
            Me.Hide()
        End If
    End Sub

    Private Sub btnSet_Click(sender As System.Object, e As System.EventArgs) Handles btnSet.Click
        My.Settings.HiberniteTime = txtHibernateTime.Text
        hibernateTimeSetting = My.Settings.HiberniteTime.Split(":")
        hibernateTime = New DateTime(now.Year, now.Month, now.Day, hibernateTimeSetting(0), hibernateTimeSetting(1), 0)
        Timer1.Interval = 60000
        Me.Hide()
    End Sub

    Private Sub btnSetStartup_Click(sender As System.Object, e As System.EventArgs) Handles btnSetStartup.Click
        RegUtil.SetStartup("RHibernate", Application.ExecutablePath)
        MessageBox.Show("Berhasil mendaftarkan startup.")
    End Sub
#End Region

End Class
